﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OrmSmackdown.EntityFrameworks.Mappings;
using System.IO;

namespace OrmSmackdown.EntityFrameworks
{
    public class Exerciser : IExerciser
    {
        private string _connectionInformation;
        OrmSmackdown.EntityFrameworks.Mappings.efSmackDownEntities _ctx;

        public Exerciser(string connectionInformation)
        {
            _connectionInformation = connectionInformation;
            _ctx = new OrmSmackdown.EntityFrameworks.Mappings.efSmackDownEntities(_connectionInformation);
        }

        #region Insert
        public void Insert()
        {
            using (var ctx = new OrmSmackdown.EntityFrameworks.Mappings.efSmackDownEntities(_connectionInformation))
            {
                var managerRole = new Role { RoleName = Constants.ManagerRole, IsForContact = false, IsForPortal = true };
                var employeeRole = new Role { RoleName = Constants.EmployeeRole, IsForContact = false, IsForPortal = true };
                var contactRole = new Role { RoleName = Constants.ContactRole, IsForContact = true, IsForPortal = false };

                var user1 = new User { FirstName = Constants.User1FirstName, LastName = Constants.User1LastName, Email = Constants.User1Email, MyRole = managerRole };
                var user2 = new User { FirstName = Constants.User2FirstName, LastName = Constants.User2LastName, Email = Constants.User2Email, MyRole = employeeRole };
                var user3 = new User { FirstName = Constants.User3FirstName, LastName = Constants.User3LastName, Email = Constants.User3Email, MyRole = employeeRole };
                var user4 = new User { FirstName = Constants.User4FirstName, LastName = Constants.User4LastName, Email = Constants.User4Email, MyRole = managerRole };
                var user5 = new User { FirstName = Constants.User5FirstName, LastName = Constants.User5LastName, Email = Constants.User5Email, MyRole = employeeRole };

                var company1 = new Company { CompanyName = Constants.Company1 };
                var company2 = new Company { CompanyName = Constants.Company2 };

                var contact1 = new Contact { FirstName = Constants.Contact1FirstName, LastName = Constants.Contact1LastName, Email = Constants.Contact1Email, MyRole = contactRole, Company = company1 };
                var contact2 = new Contact { FirstName = Constants.Contact2FirstName, LastName = Constants.Contact2LastName, Email = Constants.Contact2Email, MyRole = contactRole, Company = company1 };
                var contact3 = new Contact { FirstName = Constants.Contact3FirstName, LastName = Constants.Contact3LastName, Email = Constants.Contact3Email, MyRole = contactRole, Company = company2 };
                var contact4 = new Contact { FirstName = Constants.Contact4FirstName, LastName = Constants.Contact4LastName, Email = Constants.Contact4Email, MyRole = contactRole, Company = company2 };
                var contact5 = new Contact { FirstName = Constants.Contact5FirstName, LastName = Constants.Contact5LastName, Email = Constants.Contact5Email, MyRole = contactRole, Company = company2 };
                var contact6 = new Contact { FirstName = Constants.Contact6FirstName, LastName = Constants.Contact6LastName, Email = Constants.Contact6Email, MyRole = contactRole, Company = company2 };

                var portal1 = new Portal { PortalName = Constants.Portal1, Administrator = user1 };
                var portal2 = new Portal { PortalName = Constants.Portal2, Administrator = user4 };

                ctx.AddToCompanies(company1);
                ctx.AddToCompanies(company2);

                ctx.AddToRoles(managerRole);
                ctx.AddToRoles(employeeRole);
                ctx.AddToRoles(contactRole);

                ctx.AddToPeople(contact1);
                ctx.AddToPeople(contact2);
                ctx.AddToPeople(contact3);
                ctx.AddToPeople(contact4);

                ctx.AddToPeople(user1);
                ctx.AddToPeople(user2);
                ctx.AddToPeople(user3);
                ctx.AddToPeople(user4);

                ctx.AddToPortals(portal1);
                ctx.AddToPortals(portal2);

                user1.MyPortals.Add(portal1);

                user2.MyPortals.Add(portal1);
                user2.MyPortals.Add(portal2);

                user3.MyPortals.Add(portal1);
                user3.MyPortals.Add(portal2);

                user4.MyPortals.Add(portal2);

                user5.MyPortals.Add(portal2);

                AddAddress(ctx, user1, "1000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, user2, "2000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, user3, "3000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, user4, "4000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, user5, "5000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, user1, "1100 Some St.", string.Empty, Constants.TownWeCareAbout, "ST", "12345", "1234", true);
                AddAddress(ctx, user2, "1200 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, user3, "1300 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, user4, "1400 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, user5, "1500 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);

                AddAddress(ctx, contact1, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, contact2, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, contact3, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, contact4, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, contact5, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, contact6, "7000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", false);
                AddAddress(ctx, contact1, "8000 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, contact2, "8100 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, contact3, "8200 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, contact4, "8300 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, contact5, "8400 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);
                AddAddress(ctx, contact6, "8500 Some St.", string.Empty, Constants.SomeOtherTown, "ST", "12345", "1234", true);

                ctx.SaveChanges();
            }
        }

        private void AddAddress(OrmSmackdown.EntityFrameworks.Mappings.efSmackDownEntities session, Person p, string line1, string line2, string city, string state, string zip, string zip4, bool isDefault)
        {
            var addr = new Address { Line1 = line1, Line2 = line2, City = city, State = state, ZipCode = zip, ZipCodePlusFour = zip4, IsDefault = isDefault, Person = p };
            session.AddToAddresses(addr);
        }
        #endregion

        public IList<string> GetPortalAdminEmails()
        {
            return (from portals in _ctx.Portals select portals.Administrator.Email).ToList();
        }

        public IList<string> GetContactEmailsForCompany(string companyName)
        {
            var contacts = from c in _ctx.People.OfType<Contact>()
                          join cmp in _ctx.Companies on c.Company equals cmp
                          where cmp.CompanyName == companyName
                              select c.Email;

            return contacts.ToList();
        }

        public int GetNumberOfPortals()
        {
            return _ctx.Portals.Count();
        }
        public int GetNumberOfUsers()
        {
            return _ctx.People.OfType<User>().Count();
        }

        public int GetNumberOfContacts()
        {
            return _ctx.People.OfType<Contact>().Count();
        }

        public string GetDefaultCity(string email)
        {
            var city = from address in _ctx.Addresses
                       join usr in _ctx.People.OfType<User>() on address.Person.PersonId equals usr.PersonId
                       where address.IsDefault == true && usr.Email == email                        
                       select address.City;

            return city.FirstOrDefault();
        }

        public string GetAdminForPortal(string portalName)
        {            
            var email = from prsn in _ctx.People.OfType<User>()
                         join prt in _ctx.Portals on prsn equals prt.Administrator
                         where prt.PortalName == portalName
                         select prsn.Email;

            return email.FirstOrDefault();
        }

        public double GetAverageUsersPerPortal()
        {
            return _ctx.Portals.Average(prt => prt.PeopleInPortal.Count);
        }

        public IList<string> GetUserEmails()
        {
            return (from peo in _ctx.People.OfType<User>() select peo.Email).ToList();
        }

        public IList<string> GetContactEmails()
        {
            return (from peo in _ctx.People.OfType<Contact>() select peo.Email).ToList();
        }

        public void Dispose()
        {
            _ctx.Dispose();
        }
    }
}
